﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;
using DungeonDelver.Engine;
using DungeonDelver.Engine.Names;
using PlayDeezGames.Common.Names;

namespace DungeonDelver.Content
{
    public class MapBuilder
    {
        public void BuildMap(Map theMap, string theTemplate,Descriptor theMapTemplates,Descriptor theMapBuilder)
        {
            var templateList = theMapBuilder[theTemplate] as StringListProperty;
            foreach (var template in templateList.Value)
            {
                var fileName = (theMapTemplates[template] as StringProperty).Value;
                XDocument doc = XDocument.Load(fileName);
                var templateMap = Map.Load(null, doc.Root);
                foreach (var mapColumn in templateMap.Columns)
                {
                    foreach (var mapRow in templateMap[mapColumn].Rows)
                    {
                        if (templateMap[mapColumn][mapRow] != null && templateMap[mapColumn][mapRow].Terrain!=null)
                        {
                            if (theMap[mapColumn] == null)
                            {
                                theMap[mapColumn] = MapColumn.Create(theMap);
                            }
                            if (theMap[mapColumn][mapRow] == null)
                            {
                                theMap[mapColumn][mapRow] = MapCell.Create(theMap[mapColumn]);
                            }
                            if (theMap[mapColumn][mapRow].Terrain == null)
                            {
                                theMap[mapColumn][mapRow].Terrain = Terrain.Create(theMap[mapColumn][mapRow]);
                            }
                            theMap[mapColumn][mapRow].Terrain.Identifier = templateMap[mapColumn][mapRow].Terrain.Identifier;
                        }
                    }
                }
            }
        }
    }
    public class MapBuilderProperty : Property
    {
        public class MapBuilderPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                MapBuilder result = new MapBuilder();
                return result;
            }
            public void SaveToXElement(System.Xml.Linq.XElement theXElement, object theObject)
            {
            }
            private MapBuilderPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.Types.MapBuilder, new MapBuilderProperty.MapBuilderPropertyPropertyFactory(), Names.Factories.MapBuilderPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.Types.MapBuilder);
            }
            public object CreateNew()
            {
                return new MapBuilder();
            }

        }
        public class MapBuilderPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new MapBuilderProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new MapBuilderProperty(theParent as Descriptor);
            }
            private MapBuilderPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.Factories.MapBuilderPropertyFactory, new MapBuilderProperty.MapBuilderPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.Factories.MapBuilderPropertyFactory);
            }
        }
        public MapBuilderProperty(Descriptor theParent, XElement theXElement)
            : base(theParent, theXElement)
        {
        }
        public MapBuilderProperty(Descriptor theParent)
            : base(theParent)
        {
        }

        public MapBuilder Builder
        {
            get
            {
                return (MapBuilder)Payload;
            }
        }
    }
}
